/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view.list;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Walter Lütgenau
 */
public class SortingInfo {

    LinkedList sortColumns = new LinkedList();

    public class SortColumn {

        public static final int DIRECTION_ASCENDING = 1;
        public static final int DIRECTION_DESCENDING = 2;
        String name;
        int direction;

        public SortColumn(String name) {
            this.name = name;
            this.direction = DIRECTION_ASCENDING;
        }

        public int getDirection() {
            return direction;
        }

        public void setDirection(int direction) {
            this.direction = direction;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public void columnClicked(String columnName) {
        SortColumn newColumn = null;
        if (sortColumns.size() > 0) {
            SortColumn sortColumn = (SortColumn) sortColumns.get(0);
            if (sortColumn.getName().equals(columnName)) {
                switch (sortColumn.getDirection()) {
                    case SortColumn.DIRECTION_ASCENDING:
                        sortColumn.setDirection(SortColumn.DIRECTION_DESCENDING);
                        break;

                    case SortColumn.DIRECTION_DESCENDING:
                        sortColumn.setDirection(SortColumn.DIRECTION_ASCENDING);
                        break;
                }
                
                newColumn = sortColumn;
            } else {
                newColumn = new SortColumn(columnName);
            }
        } else {
            newColumn = new SortColumn(columnName);
        }
        
        // remove existing sortColumn
        Iterator i = sortColumns.iterator();
        while( i.hasNext() ) {
            SortColumn sortColumn = (SortColumn) i.next();
            if ( sortColumn.getName().equals(columnName))
                i.remove();
        }
        
        sortColumns.addFirst(newColumn);
        
        propagateSortOrderChange();
    }
    
    class BooleanMap extends DummyMap {
        HashSet values = new HashSet();
        
        public BooleanMap() {
        }
        
        public BooleanMap(String value) {
            values.add(value);
        }

        @Override
        public Object get(Object key) {
            if ( values.contains(key))
                return new Boolean(true);
            return new Boolean(false);
        }
        
        public void add(Object key) {
            values.add(key);
        }
        
    }
    
    public Map getFirstColumn() {
        if ( sortColumns.size() > 0) {
            SortColumn firstSortColumn = (SortColumn) sortColumns.getFirst();
            return new BooleanMap(firstSortColumn.getName());
        }
        
        return new BooleanMap("");
    }
    
    public Map getAscending() {
        BooleanMap result = new BooleanMap();
        Iterator i = sortColumns.iterator();
        while( i.hasNext()) {
            SortColumn sortColumn = (SortColumn) i.next();
            if ( sortColumn.getDirection() == SortColumn.DIRECTION_ASCENDING)
                result.add(sortColumn.getName());
        }
    
        return result;
    }

    public List getSortColumns() {
        return sortColumns;
    }
    
    List sortOrderChangeListeners = new LinkedList();
    
    public void addSortOrderChangeListener(SortOrderChangeListener listener) {
        sortOrderChangeListeners.add(listener);
    }
    
    protected void propagateSortOrderChange() {
        Iterator i = sortOrderChangeListeners.iterator();
        while( i.hasNext()) {
            SortOrderChangeListener listener = (SortOrderChangeListener) i.next();
            listener.sortOrderChanged();
        }
    }
}
